package com.qumitech.visualnovel.component.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Config {
    private static Logger logger = LoggerFactory.getLogger(Config.class);
    public static final String ONLINE_ENV = "online";
    public static final String TEST_ENV = "test";
    public static final String DEV_ENV = "dev";

    private static String env = DEV_ENV;

    private static Environment environment;

    public static void setEnv(String env) {
        Config.env = env;
        logger.info("active env:{}", env);
    }

    public static String getEnv() {
        return env;
    }

    public static Environment getEnvironment() {
        return environment;
    }

    public static void setEnvironment(Environment environment) {
        Config.environment = environment;
        setEnv(environment.getProperty("env"));
    }

    /**
     * check is env is valid
     *
     * @param env env
     * @return bool
     */
    public static boolean isValidEnv(String env) {
        return ONLINE_ENV.equals(env) || TEST_ENV.equals(env) || DEV_ENV.equals(env);
    }

    /**
     * 检查是否为正式环境
     * @return 是否为正式环境
     */
    public static boolean isProEnv() {
        return ONLINE_ENV.equals(env);
    }

    public static boolean isDevEnv() {
        return DEV_ENV.equals(env);
    }

    public static boolean isTestEnv() {
        return TEST_ENV.equals(env);
    }

    /**
     * get property from environment
     *
     * @param key key
     * @return value
     */
    public static String getProperty(String key) {
        return Config.environment.getProperty(key);
    }
}
